<!DOCTYPE html>
<html lang="zh">
    <head>
        <meta charset="utf-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>流程表单配置</title>
        <style>
           div,th,td {
                font-size: 12px;
           }
           td a {
               margin: 0px 2px;
           }
        </style>
    </head>
    <body>
        <div id="content">
            <div id="currentUser" style="color:green;font-weight:bold;font-size:14px;"></div>
            <div><a id="backTo" href='#'>返回</a></div>

            <div>&nbsp;</div>
            <div>流程表单配置字段列表,<a id="addFiled" href='#'>增加字段</a></div>
			<table border="1" id="flowlst">
            </table>
        </div>
        <script src="js/config.js"></script>
        <script src="js/jquery.min.js"></script>
        <script type="text/javascript">
            var contextPath = getContextPath();
            var userId = getQueryVariable("userId");
            var orderType = getQueryVariable("orderType");

            //返回按钮
            document.getElementById("backTo").href = "./main.html?userId="+userId;
            
            //修改增加字段按钮的href
            document.getElementById("addFiled").href = "./flowFormAddOrEdit.html?userId=lix&orderType="+orderType;

            $(function() {
                //模拟所有参与人员
                //lix：普通人员
                //zhaoyl：项目经理
                //liujh,linjh,huangjc：部门经理
                //xuw：总经理
                let arr = ['lix', 'zhaoyl', 'liujh', 'linjh', 'huangjc', 'xuw'];
                if(!arr.includes(userId)){
                    alert("未合法的userId，请查看main.html里面的定义.");
                    return;
                }
                //设置当前用户信息：
                document.getElementById("currentUser").innerHTML = "你是："+userId;

                //1.加载流程表单配置列表
                $.ajax({
                    url:  window.location.origin + contextPath + '/workFlowFormField/list',
                    data: JSON.stringify({
                        orderType: orderType
                    }),
                    type: 'post',
                    contentType: 'application/json;charset=utf-8',
                    dataType: "json",
                    success: function (res) {

                        var htmlStr = "<tr>";
                        htmlStr += "<th>ID</th>";
                        htmlStr += "<th>工单类型(order_type)</th>";
                        htmlStr += "<th>字段名称(field_name)</th>";
                        htmlStr += "<th>字段中文(field_cname)</th>";
                        htmlStr += "<th>字段类型(field_type)</th>";
                        htmlStr += "<th>默认值(default_value)</th>";
                        htmlStr += "<th>其他信息(other_info)</th>";
                        htmlStr += "<th>序号(field_order)</th>";
                        htmlStr += "<th>操作</th>";

                        if(res.length >0){
                            var list = res;
                            for(var i=0;i<list.length;i++){
                                htmlStr += "<tr>";
                                htmlStr += "<td>"+list[i].id+"</td>";
                                htmlStr += "<td>"+list[i].orderType+"</td>";
                                htmlStr += "<td>"+list[i].fieldName+"</td>";
                                htmlStr += "<td>"+list[i].fieldCname+"</td>";
                                htmlStr += "<td>"+list[i].fieldType+"</td>";
                                htmlStr += "<td>"+list[i].defaultValue+"</td>";
                                htmlStr += "<td>"+list[i].otherInfo+"</td>";
                                htmlStr += "<td>"+list[i].fieldOrder+"</td>";
                                
                                //按钮列表
                                htmlStr += "<td>";
                                htmlStr += "<a href='./flowFormAddOrEdit.html?orderType="+orderType+"&userId="+userId+"&id="+list[i].id+"'>编辑</a>";
                                htmlStr += "<a href='#' onclick='deleteFlowField("+list[i].id+")'>删除</a>";
                                htmlStr += "</td>";
                                htmlStr += "</tr>";
                            
                            }
                            
                        }

                        document.getElementById("flowlst").innerHTML = htmlStr;
                        
                    },
                    error: function (result) {alert("加载流程表单配置失败，内部错误.");}
                });

            });

            //删除流程表单配置字段
            function deleteFlowField(id){
                if (window.confirm("确认删除?")) {
                    //删除流流程表单配置字段
                    $.ajax({
                        url:  contextPath + "/workFlowFormField/delete",
                        data: JSON.stringify({
                            ids: [id]
                        }),
                        type: 'post',
                        contentType: 'application/json;charset=utf-8',
                        dataType: "json",
                        success: function (res) {
                            alert("成功删除流程表单配置字段");
                            window.location.href = contextPath + "/flowFormList.html?userId="+userId+"&orderType="+orderType;

                        },
                        error: function (result) {alert("删除流程表单配置字段失败，内部错误.");}
                    });
                }               
            }

            //获取浏览器地址上的url参数
            function getQueryVariable(variable){
                var query = window.location.search.substring(1);
                var vars = query.split("&");
                for (var i=0;i<vars.length;i++) {
                    var pair = vars[i].split("=");
                    if(pair[0] == variable){return pair[1];}
                }
                return "";
            }

        </script>
    </body>
</html>